package com.atzzy.ysmall.ysmall_userserviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atzzy.ysmall.ysmall_allService.UserService.UserService;
import com.atzzy.ysmall.ysmall_beans.Users;
import com.atzzy.ysmall.ysmall_common.utils.MD5Utils;
import com.atzzy.ysmall.ysmall_common.vo.R;
import com.atzzy.ysmall.ysmall_mapper.UserMapper;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

/**
 * @Author JAVAZeng
 * @Date 2022/2/22 15:00
 * @Version 1.0
 */
@Service
@Component
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户注册接口
     */
    @Override
    public R registryTo(Users users) {
        System.out.println(this);
        Users nUsers = userMapper.selectHasUser(users.getUsername());
        if (nUsers == null) {
            nUsers = new Users();
            nUsers.setUsername(users.getUsername());//用户名
            nUsers.setPassword(MD5Utils.md5(users.getPassword()));//工具类 MD5加密
            nUsers.setUserMobile(users.getUserMobile());//用户手机号
            nUsers.setUserRegtime(new Date());
            nUsers.setUserModtime(new Date());
            nUsers.setUserImg("img/none");
            if (userMapper.userRegister(nUsers)) {
//                System.out.println(nUsers + "ghjghjh");
                return new R(200, "注册成功", null);
            } else {
                return new R(330, "注册失败", null);
            }
        } else {

//            System.out.println(nUsers + "sadasdas");
            return new R(390, "用户名已经注册", null);
        }
    }

    /**
     * 用户登录接口
     */
    @Override
    public R loginTo(Users users) {
        String em = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";//邮箱正则验证
        String ph = "^1\\d{10}$";//手机号码正则验证
        String userAccount = users.getUserAccount();
        if (userAccount.matches(ph)) {
            users.setUserMobile(userAccount);
            users.setUserAccount(null);
            Users usersP = userMapper.userLoginPhone(users.getUserMobile());
            if (userMapper.userLoginPhone(users.getUserMobile()) == null) {
                return new R(505, "登录失败,手机号不存在", null);
            } else {
                if (MD5Utils.md5(users.getPassword()).equals(usersP.getPassword())) {
                    //登录验证成功 则需要生成令牌token 按照特定的规则生成的字符串
                    JwtBuilder builder = Jwts.builder();
                    HashMap<String, Object> map = new HashMap<>();
                    String token = builder.setSubject(usersP.getUsername())
                            .setIssuedAt(new Date())//设置token的生成时间
                            .setId(usersP.getUserId() + "")//设置用户id为tokenid
                            .setClaims(map)//map中可以存放用户的角色信息
                            .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))//设置过期时间
                            .signWith(SignatureAlgorithm.HS256, "zzyYYDS").compact();//设置加密方式和加密密码
                    return new R(201, token, usersP);
                } else {
                    return new R(501, "登录失败，密码错误", null);
                }
            }
        } else if (userAccount.matches(em)) {
            users.setUserEmail(userAccount);
            users.setUserAccount(null);
            Users usersE = userMapper.userLoginEmail(users.getUserEmail());
            if (userMapper.userLoginEmail(users.getUserEmail()) == null) {
                return new R(506, "登录失败,邮箱不存在", null);
            } else {
                if (MD5Utils.md5(users.getPassword()).equals(usersE.getPassword())) {

                    JwtBuilder builder = Jwts.builder();
                    HashMap<String, Object> map = new HashMap<>();
                    String token = builder.setSubject(usersE.getUsername())
                            .setIssuedAt(new Date())//设置token的生成时间
                            .setId(usersE.getUserId() + "")//设置用户id为tokenid
                            .setClaims(map)//map中可以存放用户的角色信息
                            .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))//设置过期时间
                            .signWith(SignatureAlgorithm.HS256, "zzyYYDS").compact();//设置加密方式和加密密码
                    return new R(201, token, usersE);
                } else {
                    return new R(501, "登录失败，密码错误", null);
                }
            }
        } else {
            users.setUsername(userAccount);
            users.setUserAccount(null);
            Users usersU = userMapper.userLoginName(users.getUsername());
            if (userMapper.userLoginName(users.getUsername()) == null) {
                return new R(507, "登录失败,用户名不存在", null);
            } else {
                if (MD5Utils.md5(users.getPassword()).equals(usersU.getPassword())) {
                    JwtBuilder builder = Jwts.builder();
                    HashMap<String, Object> map = new HashMap<>();
                    String token = builder.setSubject(usersU.getUsername())
                            .setIssuedAt(new Date())//设置token的生成时间
                            .setId(usersU.getUserId() + "")//设置用户id为tokenid
                            .setClaims(map)//map中可以存放用户的角色信息
                            .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))//设置过期时间
                                    .signWith(SignatureAlgorithm.HS256, "zzyYYDS").compact();//设置加密方式和加密密码
                    return new R(201, token, usersU);
                } else {
                    return new R(501, "登录失败，密码错误", null);
                }
            }
        }


    }


}
